package demo2;

class MyCircularQueue3 {
    public int[] elem;
    public int front;//队头下标
    public int rear;//队尾下标
    public MyCircularQueue3(int k) {
        this.elem = new int[k+1];
    }

    //入列
    public boolean enQueue(int value) {
        //判断有没有满
        if (isFull()) return false;
        this.elem[rear] = value;
        this.rear = (rear + 1) % elem.length;
        return true;
    }

    //出列
    public boolean deQueue() {
        if (isEmpty()) return false;
        this.front = (this.front + 1) % this.elem.length;
        return true;
    }

    //获得队头元素
    public int Front() {
        if (isEmpty()) return -1;
        return this.elem[this.front];
    }

    //获得队尾元素
    public int Rear() {
        if (isEmpty()) return -1;
        if (rear == 0) {
            return this.elem[elem.length-1];
        } else {
            return this.elem[rear-1];
        }
    }
    
    public boolean isEmpty() {
        return this.front == this.rear;
    }
    
    public boolean isFull() {
        if ((this.rear+1) % this.elem.length == front) {
            return true;
        }
        return false;
    }
}

/**
 * Your MyCircularQueue object will be instantiated and called as such:
 * MyCircularQueue obj = new MyCircularQueue(k);
 * boolean param_1 = obj.enQueue(value);
 * boolean param_2 = obj.deQueue();
 * int param_3 = obj.Front();
 * int param_4 = obj.Rear();
 * boolean param_5 = obj.isEmpty();
 * boolean param_6 = obj.isFull();
 */